package com.mm.service.impl;

import com.mm.bo.AttendanceInsertBo;
import com.mm.bo.AttendanceSearchBo;
import com.mm.bo.AttendanceUpdateBo;
import com.mm.mapper.AttendanceInfoMapper;
import com.mm.mapper.LogsInfoMapper;
import com.mm.org.n3r.idworker.Sid;
import com.mm.pojo.AdministratorInfo;
import com.mm.pojo.AttendanceInfo;
import com.mm.pojo.LogsInfo;
import com.mm.redis.RedisService;
import com.mm.rediskey.AdminKey;
import com.mm.service.AttendanceInfoService;
import com.mm.util.PageUtil;
import com.mm.vo.AttendanceInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName AttendanceInfoServiceImpl
 * @Description 员工考勤 Service实现类
 * @Author 任昊
 * @Date 2020/12/4 13:49
 */
@Service
public class AttendanceInfoServiceImpl implements AttendanceInfoService {

    @Resource
    private AttendanceInfoMapper attendanceInfoMapper;

    @Resource
    private Sid sid;

    @Resource
    private RedisService redisService;

    @Resource
    private LogsInfoMapper logsInfoMapper;

    /**
     * 员工员工考勤查询
     * @param attendanceSearchBo
     * @return
     */
    @Override
    public PageUtil<AttendanceInfoVo> selectAttendanceInfo(AttendanceSearchBo attendanceSearchBo) {
        Integer count = attendanceInfoMapper.findAttendanceCount(attendanceSearchBo);
        List<AttendanceInfoVo> attendanceVo = attendanceInfoMapper.findAttendanceInfo(attendanceSearchBo);
        PageUtil<AttendanceInfoVo> pageUtil = new PageUtil<>(count, count, attendanceSearchBo.getPageIndex(), attendanceSearchBo.getPageSize(), attendanceVo);
        return pageUtil;
    }

    /**
     * 员工考勤修改
     * @param attendanceUpdateBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateAttendanceInfo(AttendanceUpdateBo attendanceUpdateBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, attendanceUpdateBo.getToken(), AdministratorInfo.class);

        AttendanceInfo attendanceInfo = new AttendanceInfo();
        BeanUtils.copyProperties(attendanceUpdateBo,attendanceInfo);
        Example example = new Example(AttendanceInfo.class);
        example.createCriteria().andEqualTo("atteId", attendanceUpdateBo.getAtteId());
        int row = attendanceInfoMapper.updateByExampleSelective(attendanceInfo, example);

        long timeMillis = System.currentTimeMillis();
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "修改了考勤：" + attendanceUpdateBo.getAtteId();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return row;
    }

    /**
     * 新增员工考勤
     * @param attendanceInsertBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertAttendanceInfo(AttendanceInsertBo attendanceInsertBo) {
        //获取当前登录的管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, attendanceInsertBo.getToken(), AdministratorInfo.class);

        AttendanceInfo attendanceInfo = new AttendanceInfo();
        BeanUtils.copyProperties(attendanceInsertBo,attendanceInfo);
        long timeMillis = System.currentTimeMillis();
        attendanceInfo.setAtteId(sid.nextShort());
        attendanceInfo.setAtteCreateTime(timeMillis);
        attendanceInfo.setAtteUpdateTime(timeMillis);
        int row = attendanceInfoMapper.insertSelective(attendanceInfo);

        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "新增了考勤：" + attendanceInsertBo.getEmpId();
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return row;
    }
}
